Skip to content

Fix stale cache-hit lifecycle claims in middleware doc comments - #915

Merged
wmadden-electric merged 1 commit into
mainfrom
examples/middleware-lifecycle-doc-fixes
Jul 15, 2026
Merged

Fix stale cache-hit lifecycle claims in middleware doc comments#915
wmadden-electric merged 1 commit into
mainfrom
examples/middleware-lifecycle-doc-fixes

Conversation

@sorenbs

@sorenbs sorenbs commented Jul 4, 2026

Copy link
Copy Markdown
Member

Linked issue

Refs prisma/web#8008 (Prisma Next docs validation, which surfaced these stale comments). Stacked on #912 (examples/extensions-middleware-docs) — the corrected comments reference its slowQueryWarning example, so that PR must merge first. Sibling fix from the same validation pass: #913.

At a glance

// examples/prisma-next-demo/src/prisma/db.ts
middleware: [
  // Cache first: interceptors are consulted in registration order and
  // the first non-`undefined` result wins, so the cache gets first
  // claim. A hit skips only the driver call and per-row `onRow` hooks:
  // every middleware's `beforeExecute` (`lints`, `budgets`) has already
  // run before any `intercept` is consulted, and `afterExecute` still
  // fires for all of them with `source: 'middleware'`. The cache stores
  // raw rows; the runtime still runs `decodeRow` on the hit path, so
  // consumers see decoded values in both cases.
  createCacheMiddleware({ maxEntries: 1_000 }),
  lints(),
  budgets({
    maxRows: 10_000,
    defaultTableRows: 10_000,
    tableRows: { user: 10_000, post: 10_000 },
    maxLatencyMs: 1_000,
  }),
  slowQueryWarning({ thresholdMs: 250 }),
],

Before, this comment claimed a cache hit "short-circuits before any downstream middleware (lints, budgets) fires" — behavior the framework does not have.

Decision

Comment-only PR (no runtime change): correct three doc comments that describe middleware-lifecycle behavior the framework does not have.

  1. db.ts middleware-order comment — reworded to state what cache-first ordering actually buys (intercept precedence) and what a hit actually skips (the driver call and onRow, nothing else).
  2. find-user-by-id-cached.ts observability note — dropped the claim that "telemetry is wired in front of the cache in db.ts"; no telemetry middleware exists anywhere in the repo. The note now states the real mechanism (afterExecute fires for every middleware on a hit, tagged source: 'middleware') and points at slowQueryWarning, the actual observer registered in db.ts.
  3. middleware-cache package doc — the package's own hook documentation carried the same error ("the runtime skips beforeExecute, runDriver, and onRow") and is the likely origin of the stale example wording. Fixed the beforeExecute claim.

Reviewer notes

How the lifecycle actually works

The corrected wording follows the execution order that run-with-middleware.ts documents as canonical:

  1. beforeExecute runs first, for every middleware. Family runtimes call runBeforeExecuteChain between AST → plan lowering and parameter encoding (sql-runtime.ts, before-execute-chain.ts), so lints and budgets vet every execution before any cache lookup happens.
  2. intercept is consulted afterwards, in registration order; the first non-undefined result wins. That precedence is the actual reason to register the cache first.
  3. A hit skips the driver call and per-row onRow hooks — nothing else. afterExecute still fires for every middleware with source: 'middleware', which is how observers such as slow-query-warning.ts see cached reads.

What changed & evidence

Testing performed

  • pnpm --filter prisma-next-demo --filter @prisma-next/middleware-cache typecheck — passes. Comment-only change with no behavioural delta, so no test updates.

Skill update

n/a — doc comments only; no user-facing surface (CLI, public API, config, error codes) changed.

Alternatives considered

  • Make the code match the comment instead (have a cache hit actually suppress lints/budgets): rejected — running beforeExecute before parameter encoding is deliberate, so middleware that mutates ParamRef values (e.g. bulk-encrypt) stays visible to encode; run-with-middleware.ts documents this lifecycle as canonical.
  • Drop the telemetry sentence entirely rather than rewording: rejected — the underlying fact (afterExecute still fires on hits, tagged source: 'middleware') is the useful teaching point, and Add slow-query warning custom middleware example to prisma-next-demo #912's slowQueryWarning finally gives it a real referent.
  • Restrict the PR to examples/ and leave the package doc stale: rejected — it is the same false claim and the likely origin; fixing it here keeps one logical concern in one review.

Checklist

Summary by CodeRabbit

  • Documentation
    • Clarified middleware lifecycle behavior for cache hits, including which callbacks run and which execution steps are skipped.
    • Updated examples to accurately describe cache-hit observability and middleware ordering.
    • Added upgrade-guide notes identifying these as documentation-only corrections with no user action required.

@sorenbs
sorenbs requested a review from a team as a code owner July 4, 2026 00:08
@coderabbitai

coderabbitai Bot commented Jul 4, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yml

Review profile: CHILL

Plan: Pro

Run ID: 5b8413c3-ac48-4616-ba32-3aa59c9ed541

📥 Commits

Reviewing files that changed from the base of the PR and between c6b8632 and 1450f5c.

📒 Files selected for processing (5)
  • examples/prisma-next-demo/src/orm-client/find-user-by-id-cached.ts
  • examples/prisma-next-demo/src/prisma/db.ts
  • packages/3-extensions/middleware-cache/src/cache-middleware.ts
  • skills/extension-author/prisma-next-extension-upgrade/upgrades/0.14-to-0.15/instructions.md
  • skills/upgrade/prisma-next-upgrade/upgrades/0.14-to-0.15/instructions.md

📝 Walkthrough

Walkthrough

Updates cache middleware comments and demo documentation to accurately describe cache-hit hook execution. Adds corresponding entries to both 0.14-to-0.15 upgrade instruction sets. No runtime logic or public declarations changed.

Changes

Cache lifecycle documentation

Layer / File(s) Summary
Middleware lifecycle comments
packages/3-extensions/middleware-cache/src/cache-middleware.ts, examples/prisma-next-demo/src/prisma/db.ts, examples/prisma-next-demo/src/orm-client/find-user-by-id-cached.ts
Clarifies middleware ordering, cache-hit execution of beforeExecute and afterExecute, and skipping of driver and per-row processing.
Upgrade instruction records
skills/extension-author/.../instructions.md, skills/upgrade/.../instructions.md
Documents PR 915 as comments-only with no runtime, contract, or emitted-artifact changes.

Estimated code review effort: 1 (Trivial) | ~3 minutes

Possibly related PRs

  • prisma/prisma-next#912: Adds a demo slowQueryWarning middleware example covering afterExecute behavior on cache hits.

Suggested reviewers: sevinf

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately summarizes the doc-only fix to stale cache-hit lifecycle comments in middleware.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch examples/middleware-lifecycle-doc-fixes

Comment @coderabbitai help to get the list of available commands.

@github-actions

github-actions Bot commented Jul 4, 2026

Copy link
Copy Markdown

size-limit report 📦

Path Size
postgres / no-emit 157.93 KB (0%)
postgres / emit 131.71 KB (0%)
mongo / no-emit 98.71 KB (0%)
mongo / emit 89.43 KB (0%)
cf-worker / no-emit 184.61 KB (0%)
cf-worker / emit 155.81 KB (0%)

@pkg-pr-new

pkg-pr-new Bot commented Jul 4, 2026

Copy link
Copy Markdown

Open in StackBlitz

@prisma-next/extension-author-tools

npm i https://pkg.pr.new/@prisma-next/extension-author-tools@915

@prisma-next/mongo-runtime

npm i https://pkg.pr.new/@prisma-next/mongo-runtime@915

@prisma-next/family-mongo

npm i https://pkg.pr.new/@prisma-next/family-mongo@915

@prisma-next/sql-runtime

npm i https://pkg.pr.new/@prisma-next/sql-runtime@915

@prisma-next/family-sql

npm i https://pkg.pr.new/@prisma-next/family-sql@915

@prisma-next/extension-arktype-json

npm i https://pkg.pr.new/@prisma-next/extension-arktype-json@915

@prisma-next/middleware-cache

npm i https://pkg.pr.new/@prisma-next/middleware-cache@915

@prisma-next/mongo

npm i https://pkg.pr.new/@prisma-next/mongo@915

@prisma-next/extension-paradedb

npm i https://pkg.pr.new/@prisma-next/extension-paradedb@915

@prisma-next/extension-pgvector

npm i https://pkg.pr.new/@prisma-next/extension-pgvector@915

@prisma-next/extension-postgis

npm i https://pkg.pr.new/@prisma-next/extension-postgis@915

@prisma-next/postgres

npm i https://pkg.pr.new/@prisma-next/postgres@915

@prisma-next/sql-orm-client

npm i https://pkg.pr.new/@prisma-next/sql-orm-client@915

@prisma-next/sqlite

npm i https://pkg.pr.new/@prisma-next/sqlite@915

@prisma-next/extension-supabase

npm i https://pkg.pr.new/@prisma-next/extension-supabase@915

@prisma-next/target-mongo

npm i https://pkg.pr.new/@prisma-next/target-mongo@915

@prisma-next/adapter-mongo

npm i https://pkg.pr.new/@prisma-next/adapter-mongo@915

@prisma-next/driver-mongo

npm i https://pkg.pr.new/@prisma-next/driver-mongo@915

@prisma-next/contract

npm i https://pkg.pr.new/@prisma-next/contract@915

@prisma-next/utils

npm i https://pkg.pr.new/@prisma-next/utils@915

@prisma-next/config

npm i https://pkg.pr.new/@prisma-next/config@915

@prisma-next/errors

npm i https://pkg.pr.new/@prisma-next/errors@915

@prisma-next/framework-components

npm i https://pkg.pr.new/@prisma-next/framework-components@915

@prisma-next/operations

npm i https://pkg.pr.new/@prisma-next/operations@915

@prisma-next/ts-render

npm i https://pkg.pr.new/@prisma-next/ts-render@915

@prisma-next/contract-authoring

npm i https://pkg.pr.new/@prisma-next/contract-authoring@915

@prisma-next/ids

npm i https://pkg.pr.new/@prisma-next/ids@915

@prisma-next/psl-parser

npm i https://pkg.pr.new/@prisma-next/psl-parser@915

@prisma-next/psl-printer

npm i https://pkg.pr.new/@prisma-next/psl-printer@915

@prisma-next/cli

npm i https://pkg.pr.new/@prisma-next/cli@915

@prisma-next/cli-telemetry

npm i https://pkg.pr.new/@prisma-next/cli-telemetry@915

@prisma-next/config-loader

npm i https://pkg.pr.new/@prisma-next/config-loader@915

@prisma-next/emitter

npm i https://pkg.pr.new/@prisma-next/emitter@915

@prisma-next/language-server

npm i https://pkg.pr.new/@prisma-next/language-server@915

@prisma-next/migration-tools

npm i https://pkg.pr.new/@prisma-next/migration-tools@915

prisma-next

npm i https://pkg.pr.new/prisma-next@915

@prisma-next/vite-plugin-contract-emit

npm i https://pkg.pr.new/@prisma-next/vite-plugin-contract-emit@915

@prisma-next/mongo-codec

npm i https://pkg.pr.new/@prisma-next/mongo-codec@915

@prisma-next/mongo-contract

npm i https://pkg.pr.new/@prisma-next/mongo-contract@915

@prisma-next/mongo-value

npm i https://pkg.pr.new/@prisma-next/mongo-value@915

@prisma-next/mongo-contract-psl

npm i https://pkg.pr.new/@prisma-next/mongo-contract-psl@915

@prisma-next/mongo-contract-ts

npm i https://pkg.pr.new/@prisma-next/mongo-contract-ts@915

@prisma-next/mongo-emitter

npm i https://pkg.pr.new/@prisma-next/mongo-emitter@915

@prisma-next/mongo-schema-ir

npm i https://pkg.pr.new/@prisma-next/mongo-schema-ir@915

@prisma-next/mongo-query-ast

npm i https://pkg.pr.new/@prisma-next/mongo-query-ast@915

@prisma-next/mongo-orm

npm i https://pkg.pr.new/@prisma-next/mongo-orm@915

@prisma-next/mongo-query-builder

npm i https://pkg.pr.new/@prisma-next/mongo-query-builder@915

@prisma-next/mongo-lowering

npm i https://pkg.pr.new/@prisma-next/mongo-lowering@915

@prisma-next/mongo-wire

npm i https://pkg.pr.new/@prisma-next/mongo-wire@915

@prisma-next/sql-contract

npm i https://pkg.pr.new/@prisma-next/sql-contract@915

@prisma-next/sql-errors

npm i https://pkg.pr.new/@prisma-next/sql-errors@915

@prisma-next/sql-operations

npm i https://pkg.pr.new/@prisma-next/sql-operations@915

@prisma-next/sql-schema-ir

npm i https://pkg.pr.new/@prisma-next/sql-schema-ir@915

@prisma-next/sql-contract-psl

npm i https://pkg.pr.new/@prisma-next/sql-contract-psl@915

@prisma-next/sql-contract-ts

npm i https://pkg.pr.new/@prisma-next/sql-contract-ts@915

@prisma-next/sql-contract-emitter

npm i https://pkg.pr.new/@prisma-next/sql-contract-emitter@915

@prisma-next/sql-lane-query-builder

npm i https://pkg.pr.new/@prisma-next/sql-lane-query-builder@915

@prisma-next/sql-relational-core

npm i https://pkg.pr.new/@prisma-next/sql-relational-core@915

@prisma-next/sql-builder

npm i https://pkg.pr.new/@prisma-next/sql-builder@915

@prisma-next/target-postgres

npm i https://pkg.pr.new/@prisma-next/target-postgres@915

@prisma-next/target-sqlite

npm i https://pkg.pr.new/@prisma-next/target-sqlite@915

@prisma-next/adapter-postgres

npm i https://pkg.pr.new/@prisma-next/adapter-postgres@915

@prisma-next/adapter-sqlite

npm i https://pkg.pr.new/@prisma-next/adapter-sqlite@915

@prisma-next/driver-postgres

npm i https://pkg.pr.new/@prisma-next/driver-postgres@915

@prisma-next/driver-sqlite

npm i https://pkg.pr.new/@prisma-next/driver-sqlite@915

commit: 1450f5c

@tensordreams
tensordreams force-pushed the examples/extensions-middleware-docs branch from 391d7eb to 3520774 Compare July 7, 2026 12:03
Base automatically changed from examples/extensions-middleware-docs to main July 7, 2026 12:29
The prisma-next-demo db.ts comment claimed that registering the cache
first makes a hit short-circuit downstream middleware (lints, budgets),
and find-user-by-id-cached.ts referenced a telemetry middleware that
does not exist. Per run-with-middleware.ts, beforeExecute runs for
every middleware before any intercept is consulted; a hit skips only
the driver call and onRow hooks, and afterExecute still fires for all
middleware with source: middleware.

Reword both example comments accordingly, point the observability note
at the real slowQueryWarning middleware, and fix the same
beforeExecute-is-skipped claim in the middleware-cache package doc,
which likely seeded the example wording.

Found while validating the Prisma Next docs (prisma/web#8008).

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Signed-off-by: Søren Bramer Schmidt <sorenbs@gmail.com>
@wmadden-electric
wmadden-electric force-pushed the examples/middleware-lifecycle-doc-fixes branch from 9045a56 to 1450f5c Compare July 15, 2026 16:59
@wmadden-electric
wmadden-electric added this pull request to the merge queue Jul 15, 2026
Merged via the queue into main with commit dd8756a Jul 15, 2026
21 checks passed
@wmadden-electric
wmadden-electric deleted the examples/middleware-lifecycle-doc-fixes branch July 15, 2026 17:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants